package com.changcheng.explorer.event;

import com.changcheng.explorer.FileList;
import com.changcheng.explorer.R;
import com.changcheng.explorer.enums.ListStyle;
import com.changcheng.explorer.service.FileListService;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

/**
 * 图片按钮被单击事件
 * 
 * @author Administrator
 * 
 */
public class IBOnClickListener implements OnClickListener {

	@SuppressWarnings("unused")
	private static final String TAG = "IBOnClickListener";

	private FileList fileList;
	private boolean canSelect = false;

	public IBOnClickListener(FileList fileList) {
		this.fileList = fileList;
	}

	@Override
	public void onClick(View v) {
		ImageButton ib = (ImageButton) v;
		final FileListService fileListService = this.fileList.getFileListService();
		switch (ib.getId()) {
		case R.id.dirl_ib_home:
			fileListService.home();
			break;
		case R.id.dirl_ib_up:
			fileListService.up();
			break;
		case R.id.dirl_ib_back:
			fileListService.back();
			break;
		case R.id.dirl_ib_forward:
			fileListService.forward();
			break;
		case R.id.dirl_ib_operations:
			fileListService.getMainActivity().openOptionsMenu();
			break;
		case R.id.dirl_ib_view:
			// 视图选择列表
			final CharSequence[] items = {
					fileListService.getMainActivity().getString(
							R.string.view_list),
					fileListService.getMainActivity().getString(
							R.string.view_title) };
			AlertDialog.Builder builder = new AlertDialog.Builder(
					fileListService.getMainActivity());
			builder.setTitle(fileListService.getMainActivity().getString(
					R.string.view));

			int currentStyle = -1;
			if (fileListService.getListStyle() == ListStyle.LIST)
				currentStyle = 0;
			else if (fileListService.getListStyle() == ListStyle.TITLE)
				currentStyle = 1;

			builder.setSingleChoiceItems(items, currentStyle,
					new DialogInterface.OnClickListener() {
						// 列表被选择事件
						public void onClick(DialogInterface dialog, int item) {
							if (items[item].equals(fileListService
									.getMainActivity().getString(
											R.string.view_list))) {
								fileListService.setListStyle(ListStyle.LIST);
							} else if (items[item].equals(fileListService
									.getMainActivity().getString(
											R.string.view_title))) {
								fileListService.setListStyle(ListStyle.TITLE);
							}
							dialog.dismiss();
						}
					});
			AlertDialog alert = builder.create();
			alert.show();
			break;
		case R.id.dirl_ib_select:
			if (!canSelect) {
				this.fileList.setSelectState(true);
				canSelect = true;
			} else {
				this.fileList.setSelectState(false);
				canSelect = false;
			}
			break;
		}
	}

}
